﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    public class Solution162
    {
        public int FindPeakElement1(int[] nums)
        {
            int len = nums.Length;
            int left = 0, right = nums.Length - 1;
            int res = 0;
            while (left <= right)
            {
                int mid = (left + right) / 2;

                int leftNum = 0;
                int rightNum = 0;
                if (mid - 1 < 0)
                {
                    leftNum = int.MinValue;//把越界的值设为最小值不行，因为数组里面的数可能是int.MinValue=[-2147483648]
                }
                else
                {
                    leftNum = nums[mid - 1];
                }
                if (mid + 1 >= len)
                {
                    rightNum = int.MinValue;
                }
                else
                {
                    rightNum = nums[mid + 1];
                }
                if (nums[mid] > leftNum && nums[mid] > rightNum)
                {
                    res = mid;
                    break;
                }
                else
                {
                    if (nums[mid] < leftNum)
                    {
                        right = mid - 1;//峰顶在左边
                    }
                    else if (nums[mid] < rightNum)
                    {
                        left = mid + 1;//峰顶在右边
                    }
                }
            }
            return res;
        }

        public int FindPeakElement(int[] nums)
        {
            int len = nums.Length;
            int left = 0, right = nums.Length - 1;
            int res = -1;
            while (left <= right)
            {
                int mid = (left + right) / 2;

                int[] leftNum = MyGetNum(nums, mid - 1);
                int[] rightNum = MyGetNum(nums, mid + 1);
                int[] midNum = MyGetNum(nums, mid);

                if (MyCompare(midNum, rightNum) && MyCompare(midNum, leftNum))
                {
                    return mid;
                }
                else
                {
                    if (MyCompare(leftNum, midNum))//左边大于中间，或者左边和右边都大于中间
                    {
                        right = mid - 1;//峰顶在左边
                    }
                    else//右边大于中间
                    {
                        left = mid + 1;//峰顶在右边
                    }
                }
            }
            return res;
        }

        private bool MyCompare(int[] midNum, int[] leftNum)//一个数大于第二个数返回true
        {
            if (leftNum[0] == 0)//负无穷
            {
                return true;
            }
            else
            {
                return midNum[1] > leftNum[1];
            }
        }

        //根据数的index，返回一个新的值，处理边界条件和相等的时候

        private int[] MyGetNum(int[] nums, int index)
        {
            int[] res = new int[2];
            if (index < 0 || index == nums.Length)
            {
                res[0] = 0;//标记为无穷
                res[1] = 0;
            }
            else
            {
                res[0] = 1;//标记为正常
                res[1] = nums[index];
            }
            return res;
        }
    }
}